package com.wangshuo.webadmin.service.impl;

import com.wangshuo.webadmin.common.BaseStatus;
import com.wangshuo.webadmin.common.JWTUtil;
import com.wangshuo.webadmin.common.RedisConstant;
import com.wangshuo.webadmin.entity.SystemUser;
import com.wangshuo.webadmin.mapper.SystemUserMapper;
import com.wangshuo.webadmin.service.LoginService;
import com.wangshuo.webadmin.vo.login.CaptchaVo;
import com.wangshuo.webadmin.vo.login.LoginVo;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SystemUserMapper systemUserMapper;

    @Override
    public CaptchaVo getcaptchava(){
//        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
//        specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
//
//        String code = specCaptcha.text().toLowerCase();
//        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
//        String image = specCaptcha.toBase64();
//        redisTemplate.opsForValue().set(key, code, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
//        System.out.println(image);
//        System.out.println(key);
//        return new CaptchaVo(image, key);
        return null;
//        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
//        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
//        String value = specCaptcha.text().toLowerCase();
//        stringRedisTemplate.opsForValue().set(key, value, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
//        return new CaptchaVo(specCaptcha.toBase64(), key);

    }
    @Override
    public String login(LoginVo loginVo){
     if(!StringUtils.hasText(loginVo.getCaptchaCode())){
         return "验证码不存在";
     }
     String code= (String) redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
     if(code == null){
     return "123";
     }
     if(!code.equals(loginVo.getCaptchaCode().toLowerCase())){
         return "123";
     }
        SystemUser systemUser=systemUserMapper.selectOneByUserName(loginVo.getUsername());
     if(systemUser == null){
         return "123";
     }
     if(systemUser.getStatus() == 1){
         return "123";
     }
     if(!systemUser.getPassword().equals(DigestUtils.md5Hex(loginVo.getPassword()))){
         return "123";
     }
     return JWTUtil.createToken(systemUser.getId(),systemUser.getUsername());

    }
}
